package com.john.bms.controller.system;

import com.github.pagehelper.PageInfo;
import com.john.bms.model.result.RequestResult;
import com.john.bms.model.system.menu.MenuPage;
import com.john.bms.model.system.menu.SysMenu;
import com.john.bms.service.system.MenuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.john.bms.utils.CollectionUtils.map;

/**
 * @Author:张家小帅哥
 * @Date:2020/9/9 10:26
 * @Version:1.0
 * @Description:
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Resource
    private MenuService menuService;

    @RequiresPermissions("menu:view")
    @PostMapping("menuPageList")
    public RequestResult menuPageList(MenuPage page) {
        PageInfo<SysMenu> pageList = menuService.getPageList(page);
        return RequestResult.of(map(pageList, "data"));
    }

    @PostMapping("menuAdd")
    public RequestResult menuAdd(@RequestBody SysMenu menu) {
        boolean b = menuService.addMenu(menu);
        return RequestResult.of(map(b, "data"));
    }

    @GetMapping("menuInfo")
    public RequestResult menuInfo(Integer id) {
        SysMenu menuInfo = menuService.getMenuInfo(id);
        return RequestResult.of(map(menuInfo, "data"));
    }

    @PostMapping("updateMenuInfo")
    public RequestResult updateMenuInfo(@RequestBody SysMenu menu) {
        boolean result = menuService.updateMenu(menu);
        return RequestResult.of(map(result, "data"));
    }

    @RequiresPermissions("menu:delete")
    @RequestMapping("deleteMenu/{id}")
    public RequestResult deleteMenu(@PathVariable("id") String id){
        boolean result = menuService.deleteMenuById(Integer.valueOf(id));
        return RequestResult.of(map(result, "data"));
    }

    @RequiresPermissions("menu:update")
    @RequestMapping("updateStatus/{id}/{status}")
    public RequestResult updateStatus(@PathVariable("id") String id,@PathVariable("status") String status){
        boolean result = menuService.getMenuStatus(id, status);
        return RequestResult.of(map(result, "data"));
    }

}
